/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.dao;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import net.martinimix.beans.bind.BindingService;

import com.bluemartini.dna.BusinessObject;
import com.bluemartini.dna.BusinessObjectArray;
import com.bluemartini.dna.DNAList;

/**
 * Provides utility methods to simplify business action execution.
 * 
 * @author Scott Rossillo
 *
 */
public final class BusinessActionToolkit {
	
	private BindingService bindingService;
	
	/**
	 * Creates a new business action toolkit
	 * 
	 * @param bindingService the <code>BindingService</code> for this
	 * toolkit
	 */
	protected BusinessActionToolkit(BindingService bindingService) {
		this.bindingService = bindingService;
	}

	/**
	 * Returns a DNA list containing the business object equivalents of the given beans
	 * optionally wrapped in another dna list as "form data".
	 * 
	 * @param input a <code>Map</code> containing beans to be converted to 
	 * <code>BusinessObject</code>s and added the returned <code>DNAList</code>
	 * 
	 * @param wrapFormData <code>true</code> if the given <code>input</code> should be treated
	 * as "form data" and wrapped in an outer input list; <code>false</code> otherwise
	 * 
	 * @return a <code>DNAList</code> containing the given <code>input</code> beans as
	 * business objects
	 */
	public DNAList createBusinessActionInput(Map input, boolean wrapFormData) {
		
		DNAList formData = new DNAList();
		String key, type;
		Object o;
		BusinessObject businessObject;
		
		if(input == null) {
			return formData;
		}
		
		for(Iterator it = input.keySet().iterator(); it.hasNext(); ) {
			
			key = (String) it.next();
			type = getBusinessObjectType(key);
			o = input.get(key);
			
			if(o instanceof Collection) {
				formData.setBusinessObjectArrayRef(key, createArray( (Collection) o, type));
			} else {
				businessObject = bindingService.bindBusinessObject(o, type);
				formData.setBusinessObjectRef(key, businessObject);
			}
		}
		
		if(wrapFormData) {
			DNAList in = new DNAList();
			in.setList("formData", formData);
			
			return in;
		}
		
		// otherwise just return the raw input
		
		return formData;
	}
	
	/**
	 * Returns the business object type for a requested type,
	 * checking to see if an array type was specified.
	 */
	private String getBusinessObjectType(String key) {
		
		int index = key.indexOf("_ARRAY");
		
		return index > 0 ? key.substring(0, index) : key;
	}
	
	/**
	 * Creates a business object array from the contents of the given collection.
	 * 
	 * @param input the <code>Collection</code> containing beans to be converted to
	 * business objects returned in a <code>BusinessObjectArray</code>
	 * 
	 * @param type the business object type 
	 * 
	 * @return a <code>BusinessObjectArray</code> containing <code>BusinessObject</code>s
	 * of the given <code>type</code> converted from beans in the given <code>input</code>
	 */
	public BusinessObjectArray createArray(Collection input, String type) {
		
		BusinessObjectArray array = new BusinessObjectArray(input.size());
		BusinessObject businessObject;
		Object o;
		
		for(Iterator it = input.iterator(); it.hasNext(); ) {
			o = it.next();
			businessObject = bindingService.bindBusinessObject(o, type);
			array.addElement(businessObject);
		}
		
		return array;
	}
	
}
